package actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.brickred.socialauth.SocialAuthConfig;
import org.brickred.socialauth.SocialAuthManager;

import de.deltatree.social.web.filter.api.SASFHelper;
import de.deltatree.social.web.filter.api.SASFStaticHelper;


public class SocialAuthenticationAction {
/*
	socialAccessViaFilter
	socialAccessViaApi
	socialLogoutViaFilter
	socialLogoutViaApi
*/	
	String socialNetwork;
	String socialAccess;
	
	public String socialAccessViaFilter(){
		
		System.out.println("socialNetwork " + socialNetwork);
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		
		// For tracking
		socialAccess = "filter";
		session.setAttribute("socialNetwork", socialNetwork);
		session.setAttribute("socialAccess", socialAccess);

		return "success";
		
	}
	
	String urlProvider;
	
	public String socialAccessViaApi() throws Exception {
		
		System.out.println("socialNetwork " + socialNetwork);
				
		//Create an instance of SocialAuthConfgi object
		SocialAuthConfig config = SocialAuthConfig.getDefault();
		
		//load configuration. By default load the configuration from oauth_consumer.properties. 
		//You can also pass input stream, properties object or properties file name.
		config.load();
		   
		//Create an instance of SocialAuthManager and set config
		SocialAuthManager manager = new SocialAuthManager();
		manager.setSocialAuthConfig(config);
		
		// URL of YOUR application which will be called after authentication
		String successUrl= "http://www.mditaudios.com/page3";
		
		// get Provider URL to which you should redirect for authentication.
		// socialNetwork can have values "facebook", "twitter", "yahoo" etc. or the OpenID URL
		urlProvider = manager.getAuthenticationUrl(socialNetwork, successUrl);
		  
		System.out.println("urlProvider " + urlProvider);
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		
		// Store in session
		session.setAttribute("manager", manager);
		
		// For tracking
		socialAccess = "api";
		session.setAttribute("socialNetwork", socialNetwork);
		session.setAttribute("socialAccess", socialAccess);
				
		return "success";
	}


	public String socialLogoutViaFilter(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		
		socialNetwork = (String) session.getAttribute("socialNetwork");
		
		SASFHelper helper = SASFStaticHelper.getHelper(request);
		
		SocialAuthManager manager = helper.getAuthManager();

		manager.disconnectProvider(socialNetwork);

		session.removeAttribute("socialNetwork");
		session.removeAttribute("socialAccess");
		
		
		return "success";
		
	}	
	
	
	public String socialLogoutViaApi(){
		
		HttpSession session = ServletActionContext.getRequest().getSession();
		
		SocialAuthManager manager = (SocialAuthManager) session.getAttribute("manager");

		socialNetwork = (String) session.getAttribute("socialNetwork");
		
		manager.disconnectProvider(socialNetwork);
		
		session.removeAttribute("socialNetwork");
		session.removeAttribute("socialAccess");
		
		
		return "success";
	}

	

	public String getSocialNetwork() {
		return socialNetwork;
	}

	public void setSocialNetwork(String socialNetwork) {
		this.socialNetwork = socialNetwork;
	}

	public String getUrlProvider() {
		return urlProvider;
	}

	public void setUrlProvider(String urlProvider) {
		this.urlProvider = urlProvider;
	}
	
}
